

package com.magicking.apnswitch;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.preference.PreferenceManager;

/**
 * Broadcast receiver that performs switching current apn state and performs notification about this through sending
 * a broadcast message {@link com.magicking.apnswitch.ApplicationConstants#STATUS_CHANGED_MESSAGE}
 *
 * @author Pavlov Dmitry <pavlov.dmitry.n@gmail.com>
 */
public class SwitchStateReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (ApplicationConstants.CHANGE_STATUS_REQUEST.equals(intent.getAction())) {
            Bundle bundle = intent.getExtras();
            if (bundle == null || bundle.size() == 0) {//no params
                SwitchingAndMessagingUtils.switchAndNotify(context);
            } else {
                int onState = ApplicationConstants.State.ON;
                int targetState = bundle.getInt(ApplicationConstants.TARGET_APN_STATE, onState);
                int mmsTarget = bundle.getInt(ApplicationConstants.TARGET_MMS_STATE, onState);
                boolean showNotification = bundle.getBoolean(ApplicationConstants.SHOW_NOTIFICATION, true);
//                boolean disableAll = PreferenceManager.getDefaultSharedPreferences(context).getBoolean(ApplicationConstants.SETTINGS_DISABLE_ALL, false);
                ApnDao apnDao = new ApnDao(context);
//                apnDao.setDisableAllApns(disableAll);
                SwitchingAndMessagingUtils.switchIfNecessaryAndNotify(targetState, mmsTarget,
                        showNotification, context, apnDao);
            }
        }
    }
}
